package RenderLayer;

import ControllerLayer.PauseMenuController;
import ModelLayer.BattleCharacter;
import ModelLayer.Game;
import ModelLayer.Menu;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.Typeface;

import com.example.genericrpg_android.GameView;

public class PauseView extends View {
	Context activity;
	Game mModel;
	PauseMenuController controller;
	
	BattleCharacter[] battleCharacters;
	
	Menu mainMenu;
	
	private Typeface font;
	
	public Bitmap menuArrow;
	
	private Menu currentMenu;
	
	Paint paint;
	
	public PauseView(Game model, PauseMenuController controller, Context activity) {
		this.activity = activity;
		mModel = model;
		this.controller = controller;
		mainMenu = controller.getMainMenu();
		battleCharacters = model.getBattleCharacters();
		currentMenu = mainMenu;
		paint = new Paint();
		
		try {
			font = Typeface.createFromAsset(activity.getAssets(), "fonts/PetMe128.ttf");
			paint.setTypeface(font);
			paint.setTextSize(24.0f);
			menuArrow = BitmapFactory.decodeStream(activity.getAssets().open("images/menuArrow2.png"));
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@Override
	public void render(Canvas g, GameView root) {
		// TODO Auto-generated method stub
		paint.setColor(Color.WHITE);
		paint.setStrokeWidth(20);
		
		g.drawRoundRect(new RectF(0, 0, 5 + ((2 * root.getWidth() / 3) - 10 + 5),
				5 + (root.getHeight() - 10) + 5), 15, 15, paint);
		g.drawRoundRect(new RectF((2 * root.getWidth() / 3) + 2, 0,
				((2 * root.getWidth() / 3) + 8) + ((root.getWidth() / 3) - 12) + 8, 5 + (root.getHeight() - 10) + 8), 15, 15, paint);

		paint.setShader(new LinearGradient(0, 0, root.getWidth(), root.getHeight(), Color.BLUE,
				Color.BLACK, Shader.TileMode.MIRROR));
		g.drawRoundRect(new RectF(5, 5, 5 + ((2 * root.getWidth() / 3) - 10),
				5 + (root.getHeight() - 10)), 15, 15, paint);
		g.drawRoundRect(new RectF((2 * root.getWidth() / 3) + 8, 5,
				((2 * root.getWidth() / 3) + 8) + ((root.getWidth() / 3) - 12), 5 + (root.getHeight() - 10)), 15, 15, paint);
		paint.setShader(null);

		paint.setColor(Color.WHITE);
		float lineHeight = paint.getFontSpacing() + 35;
		int yOffset = 50;
		for (int i = 0; i < currentMenu.getOptions().size(); i++) {
			drawString(g, paint, currentMenu.getOptions().get(i), (2 * root.getWidth() / 3) + 45,
					yOffset, root.getWidth() / 3);
			if (currentMenu.getOptions().get(i).equals(currentMenu.getOption())) {
				g.drawBitmap(menuArrow, new Rect(0, 0,
						menuArrow.getWidth(),
						menuArrow.getHeight()), new Rect((2 * root.getWidth() / 3) + 25,
								(int)(yOffset - (lineHeight - 35)),
								(2 * root.getWidth() / 3) + 40, yOffset), paint);
			}

			yOffset += lineHeight + 15;
		}

	}
	
	public void drawString(Canvas g, Paint paint, String s, int x, int y, int width) {
		float lineHeight = paint.getFontSpacing();

		int xOffset = x;
		int yOffset = y;

		String[] words = s.split(" ");

		for (String word : words) {
			float wordWidth = paint.measureText(word + " ");

			if (xOffset + wordWidth >= x + width) {
				yOffset += lineHeight;
				xOffset = x;
			}

			g.drawText(word, xOffset, yOffset, paint);
			xOffset += wordWidth;
		}
	}
}
